package com.yc.sole.erp.biz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.yc.sole.common.constant.PubConstant;
import com.yc.sole.common.domain.BaseIndexResp;
import com.yc.sole.erp.dto.CustomerDto;
import com.yc.sole.erp.repository.CustomerRepository;
import com.yc.sole.erp.request.CustomerQueryReq;
import com.yc.sole.erp.request.CustomerReq;
import com.yc.sole.erp.response.CustomerResp;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import com.yc.sole.system.service.SequenceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 客户表 服务接口实现
 *
 * @author yizuomin
 * @date 2023-05-07
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class CustomerBiz {

    private final CustomerRepository customerRepository;
    private final SequenceService sequenceService;

    /**
     * 新增
     *
     * @param customerReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveCustomer(CustomerReq customerReq) {
        CustomerDto dto = new CustomerDto();
        String customerNo = sequenceService.generateSeqNo(PubConstant.CUSTOMER_SEQ_KEY);
        dto.setCustomerNo(customerNo);
        dto.setCustomerName(customerReq.getCustomerName());
        String firstLetter = String.valueOf(PinyinUtil.getFirstLetter(dto.getCustomerName().charAt(0))).toUpperCase(Locale.ROOT);
        dto.setFirstLetter(firstLetter);
        dto.setContacts(customerReq.getContacts());
        dto.setPhoneNum(customerReq.getPhoneNum());
        dto.setEmail(customerReq.getEmail());
        dto.setFax(customerReq.getFax());
        dto.setAddress(customerReq.getAddress());
        dto.setReceivables(customerReq.getReceivables());
        dto.setReceivedPayment(customerReq.getReceivedPayment());
        dto.setState(customerReq.getState());
        dto.setRemark(customerReq.getRemark());
        customerRepository.saveCustomer(dto);
    }

    /**
     * 修改
     *
     * @param customerReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCustomerById(CustomerReq customerReq) {
        CustomerDto dto = new CustomerDto();
        dto.setId(customerReq.getId());
        dto.setCustomerName(customerReq.getCustomerName());
        String firstLetter = String.valueOf(PinyinUtil.getFirstLetter(dto.getCustomerName().charAt(0))).toUpperCase(Locale.ROOT);
        dto.setFirstLetter(firstLetter);
        dto.setContacts(customerReq.getContacts());
        dto.setPhoneNum(customerReq.getPhoneNum());
        dto.setEmail(customerReq.getEmail());
        dto.setFax(customerReq.getFax());
        dto.setAddress(customerReq.getAddress());
        dto.setReceivables(customerReq.getReceivables());
        dto.setReceivedPayment(customerReq.getReceivedPayment());
        dto.setState(customerReq.getState());
        dto.setRemark(customerReq.getRemark());
        customerRepository.updateCustomerById(dto);
    }

    /**
     * 删除
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteCustomer(Collection<Long> ids) {
        customerRepository.deleteCustomerByIds(ids);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param customerPageReq
     * @return
     */
    public Paging<CustomerResp> selectCustomerPage(Paging<CustomerQueryReq> page, CustomerQueryReq customerPageReq) {
        Paging<CustomerDto> paging = customerRepository.selectCustomerPage(page, customerPageReq);
        return PageUtil.pageCopy(paging, CustomerResp.class);
    }

    /**
     * 列表查询
     *
     * @param customerName
     * @return
     */
    public List<CustomerResp> selectCustomerByName(String customerName) {
        List<CustomerDto> customDtoList = customerRepository.selectCustomerByName(customerName);
        return BeanUtil.copyToList(customDtoList, CustomerResp.class);
    }

    /**
     * 列表查询
     *
     * @param queryReq
     * @return
     */
    public List<BaseIndexResp<CustomerResp>> selectAllCustomerWithIndex(CustomerQueryReq queryReq) {
        List<CustomerDto> customDtoList = customerRepository.selectAllCustomer(queryReq);
        List<CustomerResp> customerRespList = BeanUtil.copyToList(customDtoList, CustomerResp.class);
        Map<String, List<CustomerResp>> customerDtoMap = customerRespList.stream().collect(Collectors.groupingBy(CustomerResp::getFirstLetter));
        return customerDtoMap.keySet().stream().map(firstLetter -> {
            List<CustomerResp> children = customerDtoMap.get(firstLetter);
            BaseIndexResp<CustomerResp> indexResp = new BaseIndexResp<>();
            indexResp.setIndex(firstLetter);
            indexResp.setChildren(children);
            return indexResp;
        }).collect(Collectors.toList());
    }

    /**
     * 通过Id查询客户
     *
     * @param customerId
     * @return
     */
    public CustomerResp selectCustomerById(Long customerId) {
        return BeanUtil.copyProperties(customerRepository.selectCustomerById(customerId), CustomerResp.class);
    }
}
